package org.example.vueadminapi2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.example.vueadminapi2.entity.TestPlan;
import org.example.vueadminapi2.mapper.TestPlanMapper;
import org.example.vueadminapi2.service.TestPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.example.vueadminapi2.service.jenkins.JobApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.HashMap;

//import org.example.vueadminapi2.jenkins.JobApi;

@Transactional
@Service
public class TestPlanServiceImpl implements TestPlanService {
/**
 * @FileName: TestPlanServiceImpl
 * @Description:
 * @Author: cyx
 * @date: 2025-02-26
 **/

    @Resource
    private TestPlanMapper TestPlanMapper;

    @Override
    public ArrayList<TestPlan> getAllTestPlan()
    {
        System.out.println("service 实现getAllTestPlan请求");
        List<TestPlan> testPlanList = TestPlanMapper.selectList(null);
        System.out.println(testPlanList);
        return (ArrayList<TestPlan>) testPlanList;
    }

    @Override
    public TestPlan getTestPlanByPlanId(Integer plan_id) {
        QueryWrapper<TestPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PlanId",plan_id);
        TestPlan testPlan = TestPlanMapper.selectOne(queryWrapper);
        if (Objects.isNull(testPlan)){
            return null;
        }
       return testPlan;
    }

    @Override
    public int CreateTestPlan(TestPlan testPlan) {
        if (Objects.isNull(testPlan)){
            return -1;
        }
        Map<String,String> param = new HashMap<>();
        param.put("parameter1","hello,world!");

        JobApi jobApi = new JobApi();
        jobApi.buildParamJob("cyx_pipeline",param);

        TestPlanMapper.insert(testPlan);
        return 0;
    }

    @Override
    public int SetTestPlanFinish(Integer plan_id) {
        //这里我不知道咋更新
        TestPlan testPlan = new TestPlan();
        testPlan.setPlanId(plan_id);
        testPlan.setTestStatus("已完成");
        int result = TestPlanMapper.updateById(testPlan);
        return result;
    }
}
